#ifndef PROTOCOLE_H_INCLUDED
#define PROTOCOLE_H_INCLUDED

#include "main_inc.h"
#include "dbtime.h"
#include "dbfile.h"

#pragma pack ( push, 1)

#define SIZ_HEADER  sizeof (dbi_header)

struct dbi_header
{
	u8 type;
	u16 len;
};

#define DBI_ERR     0xFF
#define SIZ_ERR     sizeof(dbi_error)
#define MAX_ERRSTR  0x40
struct dbi_error
{
    u32 module;
    char txt[MAX_ERRSTR];
    u32 err_code;
};

#define DBI_WELCOME 0x01
#define SIZ_WELCOME sizeof(dbi_welcome)

/// client is sending this on start to server
struct dbi_welcome
{
	u8 client_ver;                  // version of client
	u8 read_only;                   // one if readonly access will be needed
	db_time my_time;               // actual client time
	u32 hash_user_name;             // hash of user name, needed to acces database
	u32 hash_user_pass;             // --- || ----
	char client_version_txt[32];    // "DBA Client v1.0c build 222A"
};

#define DBI_RESPONSE                    0x08
#define SIZ_RESPONSE                    sizeof(dbi_w_response)
#define DBI_RESPONSE_OK                 0x01
#define DBI_RESPONSE_LOGIN_FAILED       0x02        // connection closed after this type of response
#define DBI_RESPONSE_USER_ONLY_READ     0x03

struct dbi_w_response               // response is send after client tried to login
{
	u8 response;
	db_time server_time;
	u8 server_ver;
	char server_version_txt[64];    // "DBArch Server v0.2b build 1879"
};

#define DBI_MDATA   0x03
#define SIZ_MDATA   sizeof( dbi_mdata )
// device sends data aftetr doing the measurment
struct dbi_mdata                   // measured data from each channel
{
    // 9 bits for channel ( 0 - 320 ), 7 bits for device ( 0 - 128 )
    u16 channel;                    // 7 bits from MSB - number of device       // max 128
    u32 config_crc;
    u32 config_dev_num;             // number of sensorr in configuration file
    u32 value;
    u8  flags;                      // over AL1, AL2, error code...
};

#define DBI_ACONFIG 0x04
#define SIZ_ACONFIG sizeof( dbi_ask_config )
struct dbi_askconfig
{
    // 9 bits for channel ( 0 - 320 ), 7 bits for device ( 0 - 128 )
    u16         channel;        // 7 bits from MSB - number of device       // max 128
    db_time     time;           // time when config was actual, for actual - set db_time to 0
};


enum dbi_status
{
    EMPTY   = 1,
    R       = 2,
    RW      = 3,
    ROW     = 4,
    RXW     = 5,
    RX      = 6,
    RO      = 7
};

struct dbi_maxtrix_remote
{
    u32 count;
    // [] u8  device_num;
    // [] u16  channel;
};


struct dbi_config       // configuration of each channel
{
    // 9 bits for channel ( 0 - 320 ), 7 bits for device ( 0 - 128 )
    u16         channel;        // 7 bits from MSB - number of device       // max 128
	char sens_name[24]; // name txt
	char sens_med [24]; // medium txt
	char sens_rng [24]; // range txt
	u32 sens_cfg_number;// number from configuration file of current sensor (ie. 4 CPC-2, 0-5%...)
	u32 al_1val;
	u32 al_2val;
    dbi_status status;
    u16 repet_time;     // repetition time in ms
    u32 matrix_id;      // id of remote sendback matrix
    u8 matrix_1[40];    // UW-1 matrix
    u8 matrix_2[40];    // UW-2 matrix
    dbi_maxtrix_remote r_matrix;

};


#define DBI_RCONFIG 0x07
#define SIZ_RCONFIG sizeof ( dbi_rconfig )
// server sends it in response for dbi_ask_config
struct dbi_rconfig
{
    db_time     time;           // time when config was created
    u8 is_acutal;               // 1 if configuration is actual, 0 for archival
    u8 flags;                   // error, empty config...
    dbi_config config;                 // configuration
};



#define DBI_SETAUSER    0x05
#define SIZ_SETAUSER    sizeof ( dbi_setactiveuser )
/// client sends it to set user working on dispatcher desk
struct dbi_setactiveuser
{
    char username[32];
    char userpass[32];
};

#define DBI_SYNCUSER    0x06
#define SIZ_SYNCUSER    sizeof ( dbi_syncuser )
// client sends it to renew users on start and when userlist was changed
struct dbi_syncuser
{
    char username[32];
    char userpass[32];
    u32 count;              // number of this user
    u32 users;              // all users
};








#pragma pack (pop)


#endif // PROTOCOLE_H_INCLUDED
